package com.demo102_二叉树的层序遍历;


import sun.misc.Queue;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

class TreeNode {
    int val;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }

}

public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {

        ArrayDeque<TreeNode> deque = new ArrayDeque<>();
        List<List<Integer>> list = new ArrayList<>();
        if (root==null){
            return list;
        }
        deque.add(root);
        while (deque.size()!=0){
            ArrayList<Integer> arrayList = new ArrayList<>();
            int size = deque.size();
            for (int i = 0; i <size ; i++) {
                TreeNode pop = deque.pop();
                arrayList.add(pop.val);
                if (pop.left!=null){
                    deque.add(pop.left);
                }
                if (pop.right!=null){
                   deque.add(pop.right);
                }

            }
            list.add(arrayList);
        }

        return list;
    }
}
